package fr.pascal.old;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonMap {

	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		
		ObjectMapper mapper = new ObjectMapper();

		// json => map
		String json = "{\"age\":49, \"prenom\":\"pascal\", \"couleur\":\"bleu\"}";
		Map<String, String> map = mapper.readValue(
				json, new TypeReference<HashMap<String, String>>() {}
		);
		System.out.println(json);
		System.out.println(map);
		System.out.println();
		
		json = "{\"age\":29,\"prenom\":\"pascal\",\"langages\":[\"java\",\"php\",\"python\"]}";
		Map<String, User> map2 = mapper.readValue(
				json, new TypeReference<HashMap<String, User>>() {}
		);
		System.out.println(json);
		System.out.println(map2);
		System.out.println();
		
		
		
		
		
		
		
		
		
		
		// map => json
//		map = new HashMap<String, String>();
//		map.put("prenom", "pascal");
//		map.put("age", "49");
//		json = mapper.writeValueAsString(map);
//		System.out.println(map);
//		System.out.println(json);
//		System.out.println();
		
		
		
		
		// map => json
//		Map<String, User> map2 = new HashMap<String, User>();
//		map2.put("u1", new User());
//		json = mapper.writeValueAsString(map2);
//		System.out.println(map2);
//		System.out.println(json);
//		System.out.println();
		
	}
}
